package com.justplay.mobilelocation;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.justplay.mobilelocation.UI.UserAgreementActivity;
import com.justplay.mobilelocation.bean.RegisterResultBean;
import com.justplay.mobilelocation.bean.RegisterSmsBean;
import com.justplay.mobilelocation.util.SMScountdown;
import com.justplay.mobilelocation.util.SharePreferencesUtil;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity {


    @Bind(R.id.et_userName)
    EditText et_userName;
    @Bind(R.id.et_userpwd)
    EditText et_userpwd;
    @Bind(R.id.et_sms_code)
    EditText et_sms_code;
    @Bind(R.id.tv_getSms)
    TextView tv_getSms;
    @Bind(R.id.btn_register)
    Button btn_register;
    @Bind(R.id.checkbox)
    CheckBox checkbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        getSupportActionBar().setTitle("新用户注册");
    }

    private void register() {

        new HttpUtil.Builder("users/register")
                .Params("username", et_userName.getText().toString())
                .Params("password", et_userpwd.getText().toString())
                .Params("verifyCode", et_sms_code.getText().toString())
                .Params("latitude", "")
                .Params("longitude", "")
//                .Version()//需要追加API版本号调用
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        RegisterResultBean registerResultBean = new Gson().fromJson(model, RegisterResultBean.class);
                        Toast.makeText(RegisterActivity.this, registerResultBean.getMsg(), Toast.LENGTH_SHORT).show();

                        if (registerResultBean.getFlag() == 1) {
                            startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                            SharePreferencesUtil.putString("userName", et_userName.getText().toString(), RegisterActivity.this);
                            if (registerResultBean.getResult().getAuthorizationState().equals("1")) {
                                SharePreferencesUtil.putBoolean("havedBuy",true,RegisterActivity.this);
                            }else{
                                SharePreferencesUtil.putBoolean("havedBuy", false, RegisterActivity.this);

                            }
                            finish();
                        }


                    }
                })
                .ErrorEventFunc(new ErrorFunListener() {
                    @Override
                    public void ErrorFun(Object... values) {

                    }
                })
                .post();
    }

    private void getSms() {

        new HttpUtil.Builder("sms/send")
                .Params("mobile", et_userName.getText().toString())
//                .Version()//需要追加API版本号调用
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        RegisterSmsBean registerResultBean = new Gson().fromJson(model, RegisterSmsBean.class);
                        Toast.makeText(RegisterActivity.this, registerResultBean.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                })
                .ErrorEventFunc(new ErrorFunListener() {
                    @Override
                    public void ErrorFun(Object... values) {

                    }
                })
                .post();
    }


    @OnClick({R.id.tv_getSms, R.id.btn_register, R.id.tv_arrgement})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_arrgement:
                startActivity(new Intent(RegisterActivity.this, UserAgreementActivity.class));
                break;
            case R.id.tv_getSms:
                SMScountdown smScountdown = new SMScountdown(tv_getSms, "%s秒", 60);
                smScountdown.setCountDownListener(new SMScountdown.OnCountDownListener() {
                    @Override
                    public void onStart() {
                        tv_getSms.setBackgroundResource(R.drawable.border_text_gray);
                        getSms();
                    }

                    @Override
                    public void onStop() {
                        tv_getSms.setBackgroundResource(R.drawable.border_text_blue);
                    }

                    @Override
                    public void onUpdate(int remindSecond) {

                    }
                });
                smScountdown.start();
                break;
            case R.id.btn_register:
                String et_userpwdStr = et_userpwd.getText().toString();
                if (checkbox.isChecked()) {
                    register();
                } else {
                    Toast.makeText(this, "请勾选用户协议", Toast.LENGTH_SHORT).show();
                }
                break;


        }
    }
}
